﻿using Dt261.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dt261.db
{
    class DisplayOptionDb
    {
        private readonly SQLiteConnection connection;

        public DisplayOptionDb(string dbPath)
        {
            connection = new SQLiteConnection(dbPath);
            connection.CreateTable<DisPlayOption>();
        }

        public int AddDisPlayOption(DisPlayOption m)
        {

            return connection.Insert(new DisPlayOption()
            {
                Uid = m.Uid,
                UserId = m.UserId,
        
                IsDifAcMasked = m.IsDifAcMasked,       
                IsLinkAc = m.IsLinkAc,
                IsDifBcRight = m.IsDifBcRight,
                IsLinkBc = m.IsLinkBc,

                AcRightType = m.AcRightType,// (int)PointDraw.POINT_TYPE_1;
                AcRightMaskedType = m.AcRightMaskedType,//(int)PointDraw.POINT_TYPE_10;
                AcLeftType = m.AcLeftType,//(int)PointDraw.POINT_TYPE_2;
                AcLeftMaskedType = m.AcLeftMaskedType,//(int)PointDraw.POINT_TYPE_9;

                BcRightType = m.BcRightType,//(int)PointDraw.POINT_TYPE_4;
                BcRightMaskedType = m.BcRightMaskedType,//(int)PointDraw.POINT_TYPE_7;
                BcLeftType = m.BcLeftType,//(int)PointDraw.POINT_TYPE_3;
                BcLeftMaskedType = m.BcLeftMaskedType,//(int)PointDraw.POINT_TYPE_8;

                UclPointType = m.UclPointType,// (int)PointDraw.POINT_TYPE_16;
            });
        }

        public int UpdateDisPlayOption(DisPlayOption m)
        {


            IList<DisPlayOption> list = connection.Query<DisPlayOption>("select * from DisPlayOption where id = ?", m.Id);

            if (list.Count() > 0)
            {
                return connection.Update(m);
            }
            else
            {
                return this.AddDisPlayOption(m);
            }
        }

        public IList<DisPlayOption> GetDisPlayOptions()
            => connection.Table<DisPlayOption>().ToList();

        public IList<DisPlayOption> GetDisPlayOptionByUid(string uid)
        {
            IList<DisPlayOption> list = connection.Query<DisPlayOption>("select * from DisPlayOption where uid = ?", uid);

            return list;
        }

        public IList<DisPlayOption> GetDisPlayOptionByUserId(string userid)
        {
            IList<DisPlayOption> list = connection.Query<DisPlayOption>("select * from DisPlayOption where UserId = ?", userid);

            return list;
        }

        public DisPlayOption GetOneDisPlayOptionByUid(string uid)
        {
            IList<DisPlayOption> list = connection.Query<DisPlayOption>("select * from DisPlayOption where uid = ?", uid);

            return list.Count() > 0 ? list[0] : null;
        }

        public DisPlayOption GetOneDisPlayOptionById(int id)
        {
            IList<DisPlayOption> list = connection.Query<DisPlayOption>("select * from DisPlayOption where id = ?", id);

            return list.Count() > 0 ? list[0] : null;
        }

        public DisPlayOption GetOneDisPlayOptionByUserId(string userid)
        {
            IList<DisPlayOption> list = connection.Query<DisPlayOption>("select * from DisPlayOption where UserId = ?", userid);

            if (list.Count() > 0)
                return list[0];
            else
            {
                return new DisPlayOption()
                {
  


                };
            }
        }
        public int DeleteDisPlayOption(DisPlayOption m)
        {
            return connection.Delete(m);
        }
    }
}
